import groovy.jmx.builder.JmxBuilder
import javax.management.MBeanServer
import javax.management.ObjectName

class JmxAttributeFactoryTest extends GroovyTestCase {
    def jmx
    def objName = "jmxBuilder:type=bean"

    void testInitializtion() {
        jmx = new JmxBuilder()
        assertTrue jmx != null
    }

    void setUp() {
        jmx = new JmxBuilder()
    }

    void testJmxAttributeFactoryExplicit() {
        def observed = new ExposedTestBean()
        MBeanServer parent = jmx.export {
            bean(observed) {
                name(objName)
                attribute(name: "length", desc: "Description")
                attribute(name: "color", desc: "color")
            }
        }


        assert parent != null
        assert parent.isRegistered(new ObjectName(objName))
        assert parent.getMBeanInfo(new ObjectName(objName)).getAttribute("Length")
        assert parent.getMBeanInfo(new ObjectName(objName)).getAttribute("Color")
        assert !parent.getMBeanInfo(new ObjectName(objName)).getAttribute("Id")

        parent.unregisterMBean new ObjectName(objName)
    }

    void testJmxAttributeFactoryImplied() {
        def observed = new ExposedTestBean()
        MBeanServer parent = jmx.export {
            bean(object: observed, name: objName)
        }


        assert parent != null
        assert parent.isRegistered(new ObjectName(objName))
        assert parent.getMBeanInfo(new ObjectName(objName)).getAttribute("Length")
        assert parent.getMBeanInfo(new ObjectName(objName)).getAttribute("Color")
        assert parent.getMBeanInfo(new ObjectName(objName)).getAttribute("Id")
        assert parent.getMBeanInfo(new ObjectName(objName)).getAttribute("Shape")
        assert parent.getMBeanInfo(new ObjectName(objName)).getAttribute("AccessPort")

        parent.unregisterMBean new ObjectName(objName)
    }

}